package com.seari.custody.controller;

import com.seari.custody.bean.SpecialWord;
import com.seari.custody.constant.MessageConstant;
import com.seari.custody.pojo.MessageBean;
import com.seari.custody.service.SpecialProtectionService;
import com.seari.custody.service.TechnicalAdviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.seari.custody.util.JwtTokenUtils.TOKEN_HEADER;

@RestController
@RequestMapping("/SpecialProtection")
public class SpecialProtectionController {

    @Autowired
    private SpecialProtectionService specialProtectionService;

    @Autowired
    private TechnicalAdviceService technicalAdviceService;


    /**
     * 专项保护方案列表
     */
    @RequestMapping("/createSpecialProtectionList")
    public Map<String, Object> technicalListed(@RequestBody Map<String, String> map) {
//        MessageBean messageBean = new MessageBean();
        Map<String, Object> map1=new HashMap<>();
        try {
            String uuid = map.get("uuid");
            String no=map.get("no");
            String type=map.get("type");
            SpecialWord tw = new SpecialWord();
            tw.setFileUuid(uuid);
            tw.setNo(no);
            tw.setType(type);
            List<SpecialWord> a = specialProtectionService.SpecialList(tw);

            // 得到是否为可上传专项保护方案状态
            Map<String, String> map2=new HashMap<>();
            map2.put("uuid",uuid);
            Map<String, Object> stringObjectMap = technicalAdviceService.engineeringInformation(map2);
            String flow_status = stringObjectMap.get("flow_status").toString();
            String sum=flow_status.equals("4")?"1":"0";
            map1.put("type",MessageConstant.SUCCESS_TYPE);
            map1.put("code","200");
            map1.put("message","查询专项保护方案列表成功");
            map1.put("state",sum);
            map1.put("data",a);
        } catch (Exception e) {
            map1.put("type",MessageConstant.FAIL_TYPE);
            map1.put("code","500");
            map1.put("message","查询专项保护方案列表失败");
        }
        return map1;
    }

    /**
     * 专项保护方案删除
     */
    @RequestMapping(value = "/createdelect")
    public MessageBean  createdelect(@RequestBody Map<String, String> map){
        MessageBean messageBean = new MessageBean();
        try {
            String no=map.get("no");
            //项目编号
            technicalAdviceService.specialdelect(no);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("专项保护方案删除成功");
            messageBean.setData(1);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("专项保护方案删除失败");
        }
        return messageBean;
    }


    /**
     * 专项保护方案通过|退回
     */
    @RequestMapping(value = "/SpecialProtectionPass")
    public MessageBean SpecialProtectionPass(@RequestBody Map<String, String> map){
        MessageBean messageBean=new MessageBean();
        try {
            Map<String, String> map1 = specialProtectionService.SpecialProtectionPass(map);
            String code=map1.get("code");
            String message=map1.get("message");
            messageBean.setCode(code);
            messageBean.setMessage(message);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("专项保护方案状态变更失败");
        }
        return messageBean;
    }

    /**
     * 根据UUID获取档案系统编号
     */
    @RequestMapping("/specialProtectionPlanId")
    public MessageBean specialProtectionPlanId(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, @RequestBody Map<String, String> map) {
        MessageBean messageBean = new MessageBean();
        try {
        map.put("token",token);
        Map<String, Object> map1 = specialProtectionService.specialProtectionPlanId(map);
        String code = (String) map1.get("code");
        String message = (String) map1.get("message");
        Object data = map1.get("data");
        messageBean.setCode(code);
        messageBean.setMessage(message);
        messageBean.setData(data);
        } catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("专项保护方案退回失败");
        }
        return messageBean;
    }

    /**
     * 专项保护方案流程退回变更
     */
    @RequestMapping("/specialProtectionFlow")
    public MessageBean specialProtectionFlow(@RequestHeader(value = TOKEN_HEADER, defaultValue = "") String token, @RequestBody Map<String, String> map){
        MessageBean messageBean = new MessageBean();
        try {
            map.put("token",token);
        }catch (Exception e) {
            e.printStackTrace();
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("专项保护方案退回失败");
        }
        return messageBean;
    }

    /**
     * 专项保护方案-草稿手动上传
     */
    @RequestMapping("/uploadingSpecialProtection")
    public MessageBean uploadingSpecialProtection(@RequestParam(value="file",required=false) MultipartFile file,String uuid){
        MessageBean messageBean = new MessageBean();
        try {
            if (file==null||file.isEmpty()) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("上传文件为空");
                return messageBean;
            }
            String contentType1 = file.getContentType();
            if (!contentType1.contains("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
                messageBean.setType(MessageConstant.FAIL_TYPE);
                messageBean.setCode(MessageConstant.FAIL);
                messageBean.setMessage("上传文件不为docx，请重新上传");
                return messageBean;
            }
            specialProtectionService.uploadingSpecialProtection(file,uuid);
            messageBean.setType(MessageConstant.SUCCESS_TYPE);
            messageBean.setCode(MessageConstant.SUCCESS);
            messageBean.setMessage("专项保护方案上传成功");
        }catch (Exception e) {
            messageBean.setType(MessageConstant.FAIL_TYPE);
            messageBean.setCode(MessageConstant.FAIL);
            messageBean.setMessage("专项保护方案上传失败");
        }
        return messageBean;
    }



}
